package com.yunliuchain.examination.common.bean;

import java.util.ArrayList;
import java.util.List;

public class Pagination
{
	private Integer page = 1;
	
	private Long total = 1l;
	
	private Integer pageSize = 10;
	
	private Long totalPages = 1l;
	
	private Integer segmentCount = 9;
	
	private Boolean hasPrevious = false;
	
	private Boolean hasNext = false;
	
	private Boolean isFirst = true;
	
	private Boolean isLast = true;
	
	private Integer pre;
	
	private Integer next;
	
	private Integer first;
	
	private Integer last;
	
	private List<Integer> segment = new ArrayList<Integer>();
	
	public Pagination()
	{
		init();
	}
	
	public Pagination(Integer page, Integer pageSize, Long total){
		setPage(page);
		setTotal(total);
		setPageSize(pageSize);
		Long totalPages = getTotal() % getPageSize() == 0 ? getTotal() / getPageSize() : getTotal() / getPageSize() + 1;
		setTotalPages(totalPages);
		init();
	}

	public Pagination(Integer page, Long totalPages, Integer segmentCount)
	{
		setTotalPages(totalPages);
		setPage(page);
		setSegmentCount(segmentCount);
		init();
	}
	

	public Integer getPageSize()
	{
		return pageSize;
	}

	public void setPageSize(Integer pageSize)
	{
		if (pageSize != null && pageSize > 0)
		{
			this.pageSize = pageSize;
		}
		
	}

	public Long getTotal()
	{
		return total;
	}

	public void setTotal(Long total)
	{
		this.total = total;
	}

	private void init()
	{
		    this.hasPrevious = this.page > 1;
		    this.hasNext = this.page < this.totalPages;
		    this.isFirst = this.page == 1;
		    this.isLast = this.page ==this.totalPages.intValue();
		    this.pre = this.page - 1;
		    this.next = this.page + 1;
		    this.first = 1;
		    this.last = this.totalPages.intValue();
		    int start = this.page - (int)Math.floor((this.segmentCount - 1) / 2.0D);
		    int end = this.page + (int)Math.ceil((this.segmentCount - 1) / 2.0D);
		    if (start < 1)
		      start = 1;
		    if (end > this.totalPages)
		      end = this.totalPages.intValue();
		    for (Integer i = start; i <= end; i++)
		    	this.segment.add(Integer.valueOf(i));
	}


	public Integer getPage()
	{
		return page;
	}

	public void setPage(Integer page)
	{
		if (page != null && page > 0)
		{
			this.page = page;
		}
	}

	public Long getTotalPages()
	{
		return totalPages;
	}

	public void setTotalPages(Long totalPages)
	{
		if (totalPages != null && totalPages > 0)
		{
			this.totalPages = totalPages;
		}
	}

	public Integer getSegmentCount()
	{
		return segmentCount;
	}

	public void setSegmentCount(Integer segmentCount)
	{
		if (segmentCount!= null && page > 0)
		{
			this.segmentCount = segmentCount;
		}
	}


	public Boolean getHasPrevious()
	{
		return hasPrevious;
	}

	public void setHasPrevious(Boolean hasPrevious)
	{
		this.hasPrevious = hasPrevious;
	}

	public Boolean getHasNext()
	{
		return hasNext;
	}

	public void setHasNext(Boolean hasNext)
	{
		this.hasNext = hasNext;
	}

	public Boolean getIsFirst()
	{
		return isFirst;
	}

	public void setIsFirst(Boolean isFirst)
	{
		this.isFirst = isFirst;
	}

	public Boolean getIsLast()
	{
		return isLast;
	}

	public void setIsLast(Boolean isLast)
	{
		this.isLast = isLast;
	}

	public Integer getPre()
	{
		return pre;
	}

	public void setPre(Integer pre)
	{
		this.pre = pre;
	}

	public Integer getNext()
	{
		return next;
	}

	public void setNext(Integer next)
	{
		this.next = next;
	}

	public Integer getFirst()
	{
		return first;
	}

	public void setFirst(Integer first)
	{
		this.first = first;
	}

	public Integer getLast()
	{
		return last;
	}

	public void setLast(Integer last)
	{
		this.last = last;
	}

	public List<Integer> getSegment()
	{
		return segment;
	}

	public void setSegment(List<Integer> segment)
	{
		this.segment = segment;
	}

	@Override
	public String toString()
	{
		return "Pagination [page=" + page + ", totalPages=" + totalPages + ", segmentCount=" + segmentCount + ", hasPrevious=" + hasPrevious + ", hasNext=" + hasNext + ", isFirst="
				+ isFirst + ", isLast=" + isLast + ", pre=" + pre + ", next=" + next + ", first=" + first + ", last="
				+ last + ", segment=" + segment + "]";
	}
	
}
